18-09-2023
ggplotRSource: https://www.autodeskresearch.com/publications/samestats
base Rggplot2 packageggplot2!hist(ChickWeight$weight)
boxplot(weight ~ Diet, data = ChickWeight)
plot(x = ChickWeight$Time, y = ChickWeight$weight)
Ggplot2ggplot2Layered plotting based on the book The Grammar of Graphics by Leland Wilkinsons.
With ggplot2 you
ggplot2 then takes care of the details
ggplotggplot()
ggplot(ChickWeight)
ggplot(ChickWeight,
aes(x = Time,
y = weight))
ggplot(ChickWeight,
aes(x = Time,
y = weight)) +
geom_point()
ggplotggplot(data = , mapping = aes(
Add layers with +
Put + at the end of a line
Map aesthetics with aes()
ggplot(ChickWeight,
aes(x = Time,
y = weight)) +
geom_point() +
geom_smooth(color = "blue") +
labs(x = "Time", y = "Weight", title = "Chick weight over time") +
theme_minimal()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(ChickWeight,
aes(x = Time,
y = weight,
color = Diet)) +
geom_point(alpha = 0.5) +
labs(x = "Time", y = "Weight",
color = "Diet", title = "Chick weight over time by diet") +
theme_minimal()
There are many different types of plot you could make! Can you think of more?
library(mice) # Boys dataset
## ## Attaching package: 'mice'
## The following object is masked from 'package:stats': ## ## filter
## The following objects are masked from 'package:base': ## ## cbind, rbind
glimpse(boys)
## Rows: 748 ## Columns: 9 ## $ age <dbl> 0.035, 0.038, 0.057, 0.060, 0.062, 0.068, 0.068, 0.071, 0.071, 0.0… ## $ hgt <dbl> 50.1, 53.5, 50.0, 54.5, 57.5, 55.5, 52.5, 53.0, 55.1, 54.5, 58.5, … ## $ wgt <dbl> 3.650, 3.370, 3.140, 4.270, 5.030, 4.655, 3.810, 3.890, 3.880, 4.2… ## $ bmi <dbl> 14.54, 11.77, 12.56, 14.37, 15.21, 15.11, 13.82, 13.84, 12.77, 14.… ## $ hc <dbl> 33.7, 35.0, 35.2, 36.7, 37.3, 37.0, 34.9, 35.8, 36.8, 38.0, 40.5, … ## $ gen <ord> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ phb <ord> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ tv <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA… ## $ reg <fct> south, south, south, south, south, south, south, west, west, east,…
summary(boys)
## age hgt wgt bmi ## Min. : 0.035 Min. : 50.00 Min. : 3.14 Min. :11.77 ## 1st Qu.: 1.581 1st Qu.: 84.88 1st Qu.: 11.70 1st Qu.:15.90 ## Median :10.505 Median :147.30 Median : 34.65 Median :17.45 ## Mean : 9.159 Mean :132.15 Mean : 37.15 Mean :18.07 ## 3rd Qu.:15.267 3rd Qu.:175.22 3rd Qu.: 59.58 3rd Qu.:19.53 ## Max. :21.177 Max. :198.00 Max. :117.40 Max. :31.74 ## NA's :20 NA's :4 NA's :21 ## hc gen phb tv reg ## Min. :33.70 G1 : 56 P1 : 63 Min. : 1.00 north: 81 ## 1st Qu.:48.12 G2 : 50 P2 : 40 1st Qu.: 4.00 east :161 ## Median :53.00 G3 : 22 P3 : 19 Median :12.00 west :239 ## Mean :51.51 G4 : 42 P4 : 32 Mean :11.89 south:191 ## 3rd Qu.:56.00 G5 : 75 P5 : 50 3rd Qu.:20.00 city : 73 ## Max. :65.00 NA's:503 P6 : 41 Max. :25.00 NA's : 3 ## NA's :46 NA's:503 NA's :522
head(boys, 3)
## age hgt wgt bmi hc gen phb tv reg ## 3 0.035 50.1 3.65 14.54 33.7 <NA> <NA> NA south ## 4 0.038 53.5 3.37 11.77 35.0 <NA> <NA> NA south ## 18 0.057 50.0 3.14 12.56 35.2 <NA> <NA> NA south
tail(boys, 3)
## age hgt wgt bmi hc gen phb tv reg ## 7447 20.780 193.5 75.4 20.13 NA <NA> <NA> NA west ## 7451 20.813 189.0 78.0 21.83 59.9 <NA> <NA> NA north ## 7475 21.177 181.8 76.5 23.14 NA <NA> <NA> NA east
gg <-
boys %>%
filter(!is.na(reg)) %>%
ggplot(aes(x = hgt,
y = wgt,
shape = reg,
colour = age)) +
geom_point( alpha = 0.5) +
labs(title = "Trend for boys",
x = "Height",
y = "Weight",
shape = "Region",
colour = "Age") +
theme_minimal()
plot(gg)
facet_wrap() and facet_grid() divide figures into panels.
boys %>% ggplot(aes(x = age, y = bmi)) + geom_point() + geom_smooth() + facet_wrap(~ reg)
Easy with ggsave()
# save as pdf
ggssave("plot.pdf", myplot)
# save as png and specify dimensions
ggssave("plot.png", myplot, width = 7, height = 5, units="in")